﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmiEmuLib.AmigaConfiguration;
using System.Xml;
using System.Diagnostics;
using AmiEmuLib.Configuration.KnownRomConfig;
using TomanuExtensions;
using TomanuExtensions.Utils;

namespace AmiEmuLib.Configuration.DefaultAmigaConfigsConfig
{
    /// <summary>
    /// Default amiga configuration. Loaded from xml.
    /// </summary>
    [DebuggerDisplay("Model = {AmigaModel}, Description = {Description}")]
    public class DefaultAmigaConfig : XmlBase
    {
        /// <summary>
        /// Amiga model.
        /// </summary>
        public AmigaModel AmigaModel { get; private set; }

        /// <summary>
        /// Description.
        /// </summary>
        public string Description { get; private set; }

        /// <summary>
        /// Short description. Model + what roms.
        /// </summary>
        public string ShortDescription { get; private set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_reader"></param>
        public DefaultAmigaConfig(XmlReader a_reader)
            : base(a_reader)
        {
        }

        /// <summary>
        /// Configuration data.
        /// </summary>
        private AmigaConfig m_config;

        /// <summary>
        /// Return config clone.
        /// </summary>
        /// <returns></returns>
        public AmigaConfig GetConfigCopy()
        {
            return new AmigaConfig(m_config);
        }

        /// <summary>
        /// Do we have all required roms.
        /// </summary>
        /// <returns></returns>
        public bool RomsAvailable()
        {
            if (KnownRoms.GetAvailableDefaultKnownRom(
                    m_config.Memory.Kickstart.RomType) == null)
            {
                return false;
            }

            if (m_config.Memory.KickstartExtended.RomType != RomType.Undefined)
            {
                if (KnownRoms.GetAvailableDefaultKnownRom(
                    m_config.Memory.KickstartExtended.RomType) == null)
                {
                    return false;
                }
            }

            return true;   
        }

        protected override void ReadXml(XmlReader a_reader)
        {
            AmigaModel = a_reader.GetAttributeEnum<AmigaModel>("AmigaModel");

            a_reader.ReadStartElement(GetType().Name);

            Description = a_reader.ReadElementString("Description");
            ShortDescription = a_reader.ReadElementString("ShortDescription");

            m_config = new AmigaConfig(a_reader);

            a_reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter a_writer)
        {
            a_writer.WriteStartElement(GetType().Name);

            a_writer.WriteAttribute("AmigaModel", AmigaModel);
            a_writer.WriteElement("Description", Description);
            a_writer.WriteElement("ShortDescription", ShortDescription);

            m_config.WriteXml(a_writer);

            a_writer.WriteEndElement();
        }
    }
}
